<?php

/**
 * uniBoard ACP New IPs Ban Form
 *
 * Light and fast forum solution for web 2.0-like communities.
 *
 * @package		uniBoard
 * @author		Rafał Pitoń
 * @license		http://www.gnu.org/licenses/gpl-3.0.html
 * @since		Version 0.1
 * 
 * ------------------------------------------------------------------------
 * 
 * $Date: 2010-09-15 17:35:38 +0000 (Wed, 15 Sep 2010) $
 * $Revision: 61 $
 * $Author: rafio.xudb@gmail.com $
 */

// ------------------------------------------------------------------------

class action_new_ip_ban{
	
	/**
	 * Main class pointer
	 *
	 * @var object
	 */
	
	private $appCore		= NULL;
	
	/**
	 * Our API
	 *
	 * @var object
	 */
	
	private $api			= NULL;
	
	/**
	 * This is list of all-right ip's we are going to ban
	 *
	 * @var array
	 */
	
	private $ips		 	= array();
	
	// ------------------------------------------------------------------------

	/**
	 * Initialise Action
	 *
	 * @param object $appCore
	 */
	
	function __construct( $appCore){
		
		// Store appCore pointer
		$this -> appCore = $appCore;
				
		// Create API
		$this -> api = new api_ban( $appCore);
			
		// Update?
		if ( $appCore -> _POST( 'update') == 1)
		{
			// Errors
			$errors = array();
			
			// Grab IPS to ban
			$ips = explode( "\n", $appCore -> _POST('ban_ips'));
			
			// First Cycle - reiterate ips. Empty ones are pwt, rest are turned into SQL-friendly ones
			$ips_n = count( $ips);
			
			// Loop
			for ( $i = 0; $i < $ips_n; $i ++)
			{
				// Get IP
				$ip = trim( $ips[$i]);
				
				// Ip not empty?
				if ( isset( $ip[0]))
				{
					// Pass IP to ready array
					$this -> ips[] = $ip;
				}
			}
			
			// No Ips?
			if ( empty( $this -> ips))
			{
				$errors[] = $appCore -> lang['acp_ban_ips_empty'];
			}
			
			// Set Ban Lenght
			$this -> api -> setBanLenght( ( 3600 * $appCore -> _POST( 'ban_lenght')), $appCore -> _POST( 'ban_lenght_custom'));
						
			// Expire date error
			if ( $this -> api -> error)
			{
				$errors[] = $appCore -> lang['acp_ban_expire_incorrect'];
			}
			
			// Api All-rite?
			if ( !empty( $errors))
			{
				// Draw Errors
				$appCore -> output -> error( join( '<br />', $errors), $appCore -> lang['new_ban_ip_form']);
				
				// Repeat Form
				$this -> drawForm();
			}
			else
			{
				// Set Log
				$appCore -> logAdmin( 'log_admin_banned_ips', htmlspecialchars( join( ', ', $this -> ips)));
				
				// Insert SQL
				$insert_sql = array();
				
				// Reiterate our ips
				$ips_n = count( $this -> ips);
				
				// Reasons
				$reason_admin = $appCore -> db -> strEscape( $appCore -> _POST( 'ban_reason_admin'));
				$reason_user = $appCore -> db -> strEscape( $appCore -> _POST( 'ban_reason_user'));
				
				// Loop
				for ( $i = 0; $i < $ips_n; $i ++)
				{
					// Add User to MassInsert
					$insert_sql[] = '\'' . $appCore -> db -> strEscape( $this -> ips[$i]) . '\', \'' . $reason_admin . '\', \''. $reason_user . '\', \'' . $appCore -> time . '\', \'' . $this -> api -> ban_lenght . '\'';
				}
				
				// Execute Multiple Insert
				$appCore -> db -> multipleInsert( 'ban_ip, ban_reason_admin, ban_reason_user, ban_date, ban_lenght', $insert_sql, 'ban_filters');
				
				// Flush Cache
				class_cache::flushCache( 'bans');
				
				// Redirect us back to list
				$appCore -> output -> drawRedirect( $appCore -> adminLink( 'act=ban_ips'), $appCore -> lang['acp_bans_ip_saved']);
			}
		}
		else
		{
			// Draw Form
			$this -> drawForm();
		}
		
	}
	
	// ------------------------------------------------------------------------

	/**
	 * Draws Form
	 *
	 */
	
	function drawForm(){
	
		// Load fields templates
		$this -> appCore -> templates['form_inputs'] = new tpl_form_inputs( $this -> appCore);
				
		// Cache output
		ob_start();
				
		// Open Form
		echo $this -> appCore -> output -> openForm( $this -> appCore -> adminLink( 'act=new_ip_ban'));
		echo $this -> appCore -> drawHidden( 'update', 1);
		echo $this -> appCore -> output -> openFormBlock();
		
		// Draw First Field
		echo $this -> appCore -> output -> formItem( $this -> appCore -> lang['acp_bans_form_ips'], $this-> appCore -> drawTextArea( 'ban_ips', join( "\n", $this -> ips)), $this -> appCore -> lang['acp_bans_form_ips_expl']);
		
		// Draw Lenght Group
		echo $this -> appCore -> output -> closeFormBlock();
		echo $this -> appCore -> output -> openFormBlock( $this -> appCore -> lang['acp_bans_form_sect_lenght']);
		
		// Draw Fields
		echo $this -> appCore -> output -> formItem( $this -> appCore -> lang['acp_bans_form_lenght'], $this-> appCore -> drawList( 'ban_lenght', array(
			0 => $this -> appCore -> lang['acp_bans_form_lenght_0'],
			'0.5' => $this -> appCore -> lang['acp_bans_form_lenght_1'],
			1 => $this -> appCore -> lang['acp_bans_form_lenght_2'],
			6 => $this -> appCore -> lang['acp_bans_form_lenght_3'],
			24 => $this -> appCore -> lang['acp_bans_form_lenght_4'],
			168 => $this -> appCore -> lang['acp_bans_form_lenght_5'],
			336 => $this -> appCore -> lang['acp_bans_form_lenght_6'],
			720 => $this -> appCore -> lang['acp_bans_form_lenght_7']
		), $this -> appCore -> _POST( 'ban_lenght')));
		echo $this -> appCore -> output -> formItem( $this -> appCore -> lang['acp_bans_form_lenght_custom_or'], $this-> appCore -> drawTextInput( 'ban_lenght_custom', $this -> appCore -> _POST( 'ban_lenght_custom')), $this -> appCore -> lang['acp_bans_form_lenght_custom_expl']);
		
		// Draw Reasons Group
		echo $this -> appCore -> output -> closeFormBlock();
		echo $this -> appCore -> output -> openFormBlock( $this -> appCore -> lang['acp_bans_form_sect_reason']);
		
		// Draw Fields
		echo $this -> appCore -> output -> formItem( $this -> appCore -> lang['acp_bans_form_reason_admin'], $this-> appCore -> drawTextArea( 'ban_reason_admin', $this -> appCore -> _POST( 'ban_reason_admin')));
		echo $this -> appCore -> output -> formItem( $this -> appCore -> lang['acp_bans_form_reason_user'], $this-> appCore -> drawTextArea( 'ban_reason_user', $this -> appCore -> _POST( 'ban_reason_user')));
		
		// Close Form
		echo $this -> appCore -> output -> submitButton( $this -> appCore -> lang['new_ban_ip_form_submit']);
		echo $this -> appCore -> output -> closeFormBlock();
		echo $this -> appCore -> output -> closeForm();
		
		// Draw output
		$this -> appCore -> output -> draw( $this -> appCore -> lang['new_ban_ip_form'], array(
			$this -> appCore -> adminLink( 'act=ban_ips') => $this -> appCore -> lang['acp_ban_ips'],
			'' => $this -> appCore -> lang['new_ban_ip_form']
		), ob_get_clean());
			
	}

}